package com.good.child.ui.activity.constants;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.good.child.MainActivity;
import com.good.child.R;
import com.good.child.bean.constants.ConstantsBean;
import com.good.child.iview.ConstantsIView;
import com.good.child.pressent.ConstantsPresenter;
import com.good.child.ui.adapter.constants.ConstantsListAdapter;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.tool.TActivityUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create on: 2024/8/4
 * by Mr.Zhu
 * Description: TODO
 */
public class ConstantsListActivity extends BaseActivity<ConstantsIView, ConstantsPresenter> implements ConstantsIView {
   public int REQUEST_CALL_PHONE = 100001;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;


    private ConstantsListAdapter adapter;
    @Override
    protected ConstantsPresenter initPresenter() {
        return new ConstantsPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activtiy_constants_list);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        setStatusBar(true, true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setAndroidNativeLightStatusBar(this, true);
        setViewLineVisable(false);
        setTitle("联系人");

        adapter = new ConstantsListAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
//        recyclerView.addItemDecoration(new ShrinkingDividerItemDecoration(1, Color.GRAY));
        mPresenter.getConstantsList();
        adapter.setOnItemClick(new ConstantsListAdapter.onItemClick() {
            @Override
            public void onClickView(String name) {
                if (ContextCompat.checkSelfPermission(ConstantsListActivity.this, Manifest.permission.CALL_PHONE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(ConstantsListActivity.this, new String[]{
                            Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE);
                } else {
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:"+name));
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onSuccess(List<ConstantsBean> dataList) {
        adapter.setData(dataList);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            TActivityUtils.jumpToActivity(getBaseContext(), MainActivity.class);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
